<?php
// ESSENTIAL VARS
$error = '';
$data_dir = 'data/';
$blog_config = $data_dir . 'config.ini';
if ($configurations = parse_ini_file($blog_config)); 
else $error .= '<em>Could not load configuration file! Check permissions on data/config.ini</em>';

$blog_db = $data_dir . 'posts.ini';

$blog_title = $configurations['blog_title'];
$blog_author = $configurations['blog_author'];
$blog_url  = $configurations['blog_url'];

$blog_controller = $configurations['blog_controller'];

$blog_heading_st_tag = '<' . $configurations['tag'] . '>';
$blog_heading_end_tag = '</' . $configurations['tag'] . '>';

$blog_posts_per_page = $configurations['ppp'];

$blog_guid = "60a76c80-d399-11d9-b93C-0003939e0af6";

/* -----------------GET BASIC DATA--------------------*/
// load in a data file
if($data = parse_ini_file($blog_db, true));
else $error .= '<em>Could not load blog posts! You may not have any, or incorrect permissions on data/posts.ini</em>';

// number stuff	
$number_of_entries = count($data);
krsort($data);

// allows for sequential access as well as random access -- ?? MARK FOR DEPRECATION

/* $n=0;
foreach($data as $unix_time => $array) {
		$numbered[$n] = $array;
		$numbered[$n]['id'] = $unix_time;
		$n++;		
}

*/


/* ------------------------------ BEGIN FUNCTION DECLARATIONS ----------------------------------*/
function parse($content) {
	// startup
	$regexURL = "((http|https|ftp|mailto):\/\/[A-Za-z0-9\.\:\@\?\&\~\%\=\+\-\/\_\;\#]+)";
	$content = stripslashes(str_replace("<","&lt;",str_replace(">","&gt;","\n".$content)));

	// php-specific
	$content = str_replace("\r\n","\n",$content);

	// pictures [url]
	$content = preg_replace("/\[($regexURL\.(png|gif|jpg|PNG|GIF|JPG|jpeg|JPG)) ?([^]]+)?\]/i","<img src=\"\\1\" class=\"wikiimage\" alt=\"\\5\"/>",$content);
    
	// [url link] external links
	$content = preg_replace("/\[$regexURL ([^]]+)\]/i","<a href=\"\\1\">\\3</a>", $content);
	
	// plain urls in the text
	$content = preg_replace("/(?<![\"\[])$regexURL(?!\")/","<a href=\"\\0\">\\0</a>",$content);

	// lists <ul>
	$content = preg_replace("/(?<=\n)\* (.+)\n/","<uli>\\1</uli>",$content);
	$content = preg_replace("/<uli>(.+)\<\/uli>/","</p><ul>\\0</ul><p>\n",$content);
	// lists <ol>
	$content = preg_replace("/(?<=\n)# (.+)\n/","<oli>\\1</oli>",$content);
	$content = preg_replace("/<oli>(.+)\<\/oli>/","</p><ol>\\0</ol><p>\n",$content);
    // ... cleaning lists
	$content = str_replace("<oli>","<li>",$content);
	$content = str_replace("</oli>","</li>",$content);
	$content = str_replace("<uli>","<li>",$content);
	$content = str_replace("</uli>","</li>",$content);
    
	// text decorations 
	$content = preg_replace("/\*([^\n]+)\*/U","<strong>\\1</strong>", $content);
	$content = preg_replace("/\*\*([^\n]+)\*\*/U","<strong>\\1</strong>", $content);	
	$content = preg_replace("/\_([^\n]+)\_/U","<em>\\1</em>", $content);	
	$content = preg_replace("/\[\[(.+)\]\]/","<span class=\"sidenote\">\\1</span>",$content); 

	// Headlines <h1><h2><h3>
	$content = preg_replace("/\n(&equals;&equals;&equals;)(.+)\n/","</p><h4>\\2</h4><p>\n",$content);
	$content = preg_replace("/\n(&equals;&equals;)(.+)\n/","</p><h3>\\2</h3><p>\n",$content);
	$content = preg_replace("/\n(&equals;)(.+)\n/","</p><h2>\\2</h2><p>\n",$content);
	
	$content = preg_replace("/\n/","</p><p>",$content);

	
	// strip leading and ending line breaks
	$content = preg_replace("/^(\n+)/","",$content); 
	$content = preg_replace("/\n{3,}/","\n",$content); 
	return $content;
}

/* SHOW LOTS */
function show_lots($start, $end) {
//	global $numbered;
	global $data;
	global $blog_heading_st_tag;	
	global $blog_heading_end_tag;
	global $number_of_entries;	
	if ($end > $number_of_entries) { $end = $number_of_entries; }
	//for ($i=$start; $i<$end; $i++) {

	foreach ($data as $id => $content) {
		if ($start < $end) {
			echo "\n" . $blog_heading_st_tag;
			echo $data[$id]['title'];
			echo $blog_heading_end_tag . "\n";
			
			echo '<p>'."\n";
			$content = parse($data[$id]['content']);
			
			echo $content . "\n";
			echo '</p>'."\n";
	
			echo '<span style="font-size:90%;">';
			echo date('l\, F dS Y h:i A', $data[$id]['moddate']);
			echo '</span>';
			echo "\n\n";
			$start++;
			}
	}
}

/* SHOW ONE */
function show_one($id) {
	global $data;
	global $blog_heading_st_tag;	
	global $blog_heading_end_tag;		
	if (array_key_exists($id, $data)) {
		
		$content = parse($data[$id]['content']);	
		$title = $data[$id]['title'];
		
		echo "\n" . $blog_heading_st_tag;
		echo $title;
		echo $blog_heading_end_tag . "\n";
	
		echo '<p>'."\n";
		echo $content . "\n";
		echo '</p>'."\n";

		echo '<span style="font-size:90%;">';
		echo date('l\, F dS Y h:i A', $id);
		echo '</span>';
		echo "\n\n";
		

	}
	else echo "<p><br /></p><div style=\"text-align:center;\"><em>Post ID</em> <strong>$id</strong><em> not found!</em></div>";
}


/* SHOW PAGE */
function show_page($page) {
	global $blog_posts_per_page;
	show_lots(($page-1) *$blog_posts_per_page, $page*$blog_posts_per_page);
}

/* SHOW PAGE NAV */
function show_nav($current_page) {
    global $blog_controller;
	global $blog_posts_per_page;
	global $number_of_entries;
	
	echo '<div class="nav" style="text-align:center;">';
        echo "page: ";
        $total_pages = ceil($number_of_entries/$blog_posts_per_page);
        if ($current_page != 1) {
                        echo '<a href="index.php?page=';
                        echo ($current_page-1);
                        echo '">&nbsp;&laquo; Prev&nbsp;</a>'."\n";
        }
        for ($i=1; $i<($total_pages+1); $i++) {
                if ($current_page == $i) {
                        echo '<span class="current">'.$i.'</span>';
                }
                else { echo '<a href="index.php?page='.$i.'">'.$i.'</a>'."\n";}
        }
        if ($current_page < $total_pages){
        echo '<a href="index.php?page=';
                        echo ($current_page+1);
        echo '">&nbsp;Next &raquo;&nbsp;</a>'."\n";
        }
      echo '</div>';
}


/* =================AGREGATION FUNCTIONS=================*/
// SHOW ATOM
function show_atom() {
	global $numbered;
	global $blog_url;
	global $blog_posts_per_page;
	for ($i=0; $i<$blog_posts_per_page; $i++) {
		echo "\n".'<entry>'."\n";
		
		echo "\t".'<title type="html">';
		echo $numbered[$i]['title'];
		echo '</title>'."\n";
		
		echo "\t".'<link href="';
		echo $blog_url . 'index.php?post=' . $numbered[$i]['id'] . '" />'."\n";
		
		echo "\t".'<updated>';
		echo date(DATE_ATOM, $numbered[$i]['id']);
		echo '</updated>'."\n";
		
		echo "\t".'<id>';
		echo $blog_url . 'index.php?post=' . $numbered[$i]['id'] . '</id>'."\n";
		
		echo "\t".'<summary type="html">';
		
		echo htmlspecialchars(parse($numbered[$i]['content']));
		echo '</summary>'."\n";
		
		echo '</entry>';
	}
}


/* VERSION PRINTER */
function print_version(){
	echo "joleblog 0.8 blu";
}


?>